import { singleton, Disposable, container } from 'tsyringe'
import { ROBOT_APP, RobotApp, createRobotApp, createRobotSystemApp } from './app';
import { ROBOT_APP_PAGE } from './page';
import { Context } from './context';
import * as pages from './apps/index';

@singleton()
export class Robot implements Disposable {
    private apps: RobotApp[];
    private ctx: Context;
    constructor() {
        this.ctx = container.resolve(Context);
        Object.keys(pages).map(key => {
            const page = pages[key];
            container.register(ROBOT_APP_PAGE, {
                useFactory: (c) => c.resolve(page)
            });
        });
        container.register(ROBOT_APP, { useFactory: createRobotApp(`org.autojs.autoxjs.v6`, `AutoXJs`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.smile.gifmaker`, '快手') })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.aod`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.android.incallui`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.tsmclient`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.sker.auto`, `老哥助手`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.android.settings`, `设置`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.powerkeeper`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.android.providers.downloads.ui`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.systemAdSolution`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.android.browser`, `浏览器`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.newhome`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`grbdxc.zzcf.pebix.iwtak`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.kuaishou.nebula`, `快手急速版`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.lbe.security.miui`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.tencent.mm`, `微信`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.xiaomi.smarthome`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.packageinstaller`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.xiaomi.gamecenter`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.personalassistant`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`android`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.xiaomi.market`, `应用时长`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.miui.gallery`) })
        container.register(ROBOT_APP, { useFactory: createRobotApp(`com.vtl.biz`) })

        /**
         * 系统应用 需要恢复上一次的应用
         */
        container.register(ROBOT_APP, { useFactory: createRobotSystemApp(`com.miui.securitycenter`, '系统安全') })
        container.register(ROBOT_APP, { useFactory: createRobotSystemApp(`com.android.systemui`, `系统`) })
        container.register(ROBOT_APP, { useFactory: createRobotSystemApp(`miui.systemui.plugin`, `系统`) })

        this.apps = container.resolveAll(ROBOT_APP)
    }
    dispose(): void {
        this.apps.map(app => app.close())
    }

    onActivity(latestActivity: string, latestPackage: string) {
        /**
         * 不加事件有可能阻塞
         */
        let app = this.apps.find(app => app.getPackageName() === latestPackage)
        if (app) {
            if (app.getIsSystem()) {
                console.log(`system app ${latestPackage}--${latestActivity}`)
                latestPackage = this.ctx.getLatestPackage()
                latestActivity = this.ctx.getLatestActivity()
                if (latestActivity && latestPackage) {
                    return this.onActivity(latestActivity, latestPackage)
                }
                return;
            }
            this.ctx.setCurrentPackage(latestPackage)
            this.ctx.setCurrentActivity(latestActivity);
            this.ctx.setCurrentApp(app)
            app?.onActivity(latestActivity, latestPackage)
        }

    }
}
